package br.com.ava.core.funcionario;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import br.com.ava.core.infra.persistencia.AbstractDAO;

@SuppressWarnings("serial")
public class FuncionarioDAO extends AbstractDAO<Funcionario> {

	/** {@inheritDocs} */
	@Override
	protected Class<Funcionario> getClassePersistente() {
		return Funcionario.class;
	}

	@Transactional(isolation = Isolation.DEFAULT, rollbackFor = Exception.class)
	protected Integer obterIdAtual() {
		Criteria criteria = createCriteria();
		criteria.setProjection(Projections.max("idFuncionario"));
		return (Integer) criteria.uniqueResult();
	}

	@SuppressWarnings("unchecked")
	@Transactional(isolation = Isolation.DEFAULT, rollbackFor = Exception.class)
	public Funcionario buscarPorEmail(String email) {
		Funcionario retorno = null;
		Criteria criteria = createCriteria();
		criteria.add(Restrictions.eq("email", email));
		List<Funcionario> lista = criteria.list();
		if (lista != null && !lista.isEmpty()) {
			retorno = lista.get(0);
		}
		return retorno;
	}

	@SuppressWarnings("unchecked")
	@Transactional(isolation = Isolation.DEFAULT, rollbackFor = Exception.class)
	public Funcionario buscarPorEmailSenha(String email, String senha) {
		Funcionario retorno = null;
		Criteria criteria = createCriteria();
		criteria.add(Restrictions.eq("email", email));
		criteria.add(Restrictions.eq("senha", senha));
		List<Funcionario> lista = criteria.list();
		if (lista != null && !lista.isEmpty()) {
			retorno = lista.get(0);
		}
		return retorno;
	}

}